import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class jdbcTest {
    public static void main(String[] args) throws SQLException {
        //1.构建DataSource
        DataSource ds = new MysqlDataSource();
        ((MysqlDataSource)ds).setUrl("jdbc:mysql://127.0.0.1:3306/java110?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)ds).setUser("root");
        ((MysqlDataSource)ds).setPassword("284703");
        //2.创建连接
        Connection connection = ds.getConnection();
        //3.建立SQL
         String sql = "select * from student";
        PreparedStatement statement = connection.prepareStatement(sql);
        //4.执行SQL
        ResultSet resultSet = statement.executeQuery();
        while(resultSet.next()){
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            System.out.print("id="+id+" ");
            System.out.println("name="+name);
        }
        //5.释放资源
        resultSet.close();
        connection.close();
        statement.close();
    }
}
class inSert{
    public void insert() throws SQLException {
        //1.构建DataSource
        DataSource ds = new MysqlDataSource();
        ((MysqlDataSource)ds).setUrl("jdbc:mysql://127.0.0.1:3306/java110?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)ds).setUser("root");
        ((MysqlDataSource)ds).setPassword("284703");
        //2.创建连接
        Connection connection = ds.getConnection();
        //3.建立SQL
        String sql = "insert into student values(3,'轻烟')";
        PreparedStatement statement = connection.prepareStatement(sql);
        //4.执行SQL
        int n = statement.executeUpdate();
        System.out.println(n);
        //5.释放资源
        connection.close();
        statement.close();
    }
}
class deLect{
    public void delect() throws SQLException {
        //1.构建DataSource
        DataSource ds = new MysqlDataSource();
        ((MysqlDataSource)ds).setUrl("jdbc:mysql://127.0.0.1:3306/java110?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)ds).setUser("root");
        ((MysqlDataSource)ds).setPassword("284703");
        //2.创建连接
        Connection connection = ds.getConnection();
        //3.建立SQL
        String sql = "delect from student where name='轻烟' ";
        PreparedStatement statement = connection.prepareStatement(sql);
        //4.执行SQL
        int n = statement.executeUpdate();
        System.out.println(n);
        //5.释放资源
        connection.close();
        statement.close();
    }
}
class upDate{
    public void update() throws SQLException {
        //1.构建DataSource
        DataSource ds = new MysqlDataSource();
        ((MysqlDataSource)ds).setUrl("jdbc:mysql://127.0.0.1:3306/java110?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)ds).setUser("root");
        ((MysqlDataSource)ds).setPassword("284703");
        //2.创建连接
        Connection connection = ds.getConnection();
        //3.建立SQL
        String sql = "update student set id=3 where name='白云'";
        PreparedStatement statement = connection.prepareStatement(sql);
        //4.执行SQL
        int n = statement.executeUpdate();
        System.out.println(n);
        //5.释放资源
        connection.close();
        statement.close();
    }
}

